<?php
/**
 * Pert PHP Library
 * SQLGenerator
 * IndicativeTable Class
 *
 * @author	Emre Hasegeli <hasegeli@gmail.com>
 * @license	GNU General Public License v3
 */

namespace Pert\PostgreSQLGenerator;

require_once __DIR__ . '/Table.php';

class IndicativeTable extends Table
{
	private $indicated;

	public function __Construct($name,
			$schema,
			Array $columnArray,
			$indicated)
	{
		parent::__Construct($name,
				$schema,
				$columnArray);

		$this->indicated = $indicated;
	}

	protected function selectClauseNameCoreSQL($prefix = '')
	{
		return $this->indicated->selectClauseNameSQL('Indicated_' . $prefix);
	}

	protected function selectClausePublishedSQL($prefix = '')
	{
		$sQL = parent::selectClausePublishedSQL($prefix);

		if($unpublishedWhereClauseSQL = $this->indicated->selectClausePublishedSQL('Indicated_' . $prefix))
		{
			if($sQL)
				$sQL = '
AND ';
			$sQL .= $unpublishedWhereClauseSQL;
		}

		return $sQL;
	}

	protected function fromClauseCoreSQL($prefix = '')
	{
		return parent::fromClauseCoreSQL($prefix) . '
JOIN ' . $this->indicated->fromClauseSQL('Indicated_' . $prefix) . ' ON "Indicated_' . $prefix . $this->indicated . '".id = "' . $prefix . $this . '"."' . $this->NameColumn(). '"';
	}

	protected function whereClauseCoreSQL($prefix = '')
	{
		$sQL = parent::whereClauseCoreSQL($prefix);

		if($whereClauseSQL = $this->indicated->whereClauseSQL('Indicated_' . $prefix))
		{
			if($sQL)
				$sQL .= '
AND ' . $whereClauseSQL;
		}

		return $sQL;
	}
}
